﻿using FlowDesigner.Interfacebase;
using FlowDesigner.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FlowDesigner.ExchangeDialog
{
    /// <summary>
    /// BaseDialog.xaml 的交互逻辑
    /// </summary>
    public partial class BaseDialog : Window
    {
        private object windows;
        private Grid grid;
        public BaseDialog()
        {
            InitializeComponent();
            //DataContext = baseDialogView;
            this.Loaded += BaseDialog_Loaded;
            this.Closing += BaseDialog_Closing;
        }

        private void BaseDialog_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // 获取当前所有可见窗口（排除设计时工具窗口）
            var windowslist = App.Current.Windows.Cast<Window>()
                .Where(w => !(w.GetType().FullName.StartsWith("Microsoft.VisualStudio")))
                .ToList();
            if (windowslist[windowslist.Count - 2].Content != null&& grid.Children.Count>0)
            {
                grid.Children.Clear();
                windowslist[windowslist.Count - 2].Content = null;
            }
            windowslist[windowslist.Count - 2].Content = windows;
            this.Loaded -= BaseDialog_Loaded;
            this.Closing -= BaseDialog_Closing;
            windows = null;
            DataContext =null;//销毁数据上下文
        }

        private void BaseDialog_Loaded(object sender, RoutedEventArgs e)
        {
            // 获取当前所有可见窗口（排除设计时工具窗口）
            var windowslist = App.Current.Windows.Cast<Window>()
                .Where(w => !(w.GetType().FullName.StartsWith("Microsoft.VisualStudio")))
                .ToList();
            windows = windowslist[windowslist.Count-2].Content;
            grid = new Grid();
             Border border = new Border();
            border.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#181818"));
            border.Opacity = 0.3;
            border.CornerRadius = new CornerRadius(6);
            if (windowslist[windowslist.Count - 2].Content != null)
            {
                windowslist[windowslist.Count - 2].Content = null;
            }
            grid.Children.Add(windows as UIElement);
            grid.Children.Add(border);
            windowslist[windowslist.Count - 2].Content= grid;
        }
    }
}
